package com.lcifn.orm.mybatis.test;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.ExifSubIFDDirectory;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.attribute.BasicFileAttributeView;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * Title: ImageExtract.java<br>
 * Description:  <br>
 *
 * @author lichao
 * @date 2018/1/12 22:59
 */
public class ImageExtract {

    public static void main(String[] args) throws ImageProcessingException, IOException {
        String sourceDirectoryPath = "H:/lichao/picture/照片2015-2016";
        String targetDirectoryPath = "H:/lichao/picture/image";

        File sourceDirectory = new File(sourceDirectoryPath);
        if(!sourceDirectory.exists()){
            System.out.println(sourceDirectoryPath + " not exist");
            return;
        }

        if(!sourceDirectory.isDirectory()){
            System.out.println(sourceDirectoryPath + " not a directory");
            return;
        }

        File[] sourceFiles = sourceDirectory.listFiles();
        for(File sourceFile : sourceFiles){
            System.out.println("start copy image " + sourceFile.getName());

            // extract image original time
            Metadata metadata = ImageMetadataReader.readMetadata(sourceFile);
            ExifSubIFDDirectory directory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
            Date date = directory.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);

            Instant instant = null;
            // if original time not exist, get create time
            if(date == null){
                BasicFileAttributeView view = Files.getFileAttributeView(sourceFile.toPath(), BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
                instant = view.readAttributes().creationTime().toInstant();
            }

            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant==null?date.toInstant():instant, ZoneId.systemDefault());

            int year = localDateTime.getYear();
            int month = localDateTime.getMonth().getValue();

            // create directory year_month
            String yearAndMonthDirectoryPath = year + "_" + (month < 10 ? "0" + month : month);
            //System.out.println(yearAndMonthDirectoryPath);

            // target directory
            File targetDirectory = new File(targetDirectoryPath);
            if(!targetDirectory.exists()){
                System.out.println("create dir " + targetDirectoryPath);
                targetDirectory.mkdir();
            }

            // target directory/year_month
            File yearAndMonthDirectory = new File(targetDirectory.getPath() + File.separator + yearAndMonthDirectoryPath);
            if(!yearAndMonthDirectory.exists()){
                System.out.println("create dir " + yearAndMonthDirectory.getPath());
                yearAndMonthDirectory.mkdir();
            }

            // target directory/year_month/image name
            File targetFile = new File(yearAndMonthDirectory.getPath() + File.separator + sourceFile.getName());
            if(!targetFile.exists()){
                // copy source image to target directory
                Files.copy(sourceFile.toPath(), new FileOutputStream(targetFile));
                System.out.println("complete copy image " + sourceFile.getName() + " to dir " + yearAndMonthDirectory.getPath());
            }else{
                System.out.println("already copied image " + sourceFile.getName() + " to dir " + yearAndMonthDirectory.getPath());
            }
        }
    }

    @Test
    public void test() throws ImageProcessingException, IOException {
        Metadata metadata = ImageMetadataReader.readMetadata(new File("H:/lichao/picture/照片2015-2016/UNADJUSTEDNONRAW_thumb_21a.jpg"));

        for (Directory directory : metadata.getDirectories()) {
            for (Tag tag : directory.getTags()) {
                System.out.format("[%s] - %s = %s",
                        directory.getName(), tag.getTagName(), tag.getDescription());
                System.out.println();
            }
            if (directory.hasErrors()) {
                for (String error : directory.getErrors()) {
                    System.err.format("ERROR: %s", error);
                }
            }
        }
    }
}
